package com.lagou.homework.driver;

import com.lagou.homework.config.PropertiesConfiguration;
import com.lagou.homework.format.WordSortOutputFormat;
import com.lagou.homework.mapper.WordSortMapper;
import com.lagou.homework.module.Word;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;

public class WordSortDriver {
    private static final String OUTPUT_PATH = PropertiesConfiguration.getProperties("homework.path.output");
    private static final String INPUT_PATH = PropertiesConfiguration.getProperties("homework.path.input");

    @SneakyThrows
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration, WordSortDriver.class.getName());
        job.setJarByClass(WordSortDriver.class);
        job.setMapperClass(WordSortMapper.class);

        job.setMapOutputKeyClass(Word.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setOutputKeyClass(Word.class);
        job.setOutputValueClass(NullWritable.class);

        job.setOutputFormatClass(WordSortOutputFormat.class);

        FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));

        FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));
//        File outDir = new File(OUTPUT_PATH);
//        if (outDir.exists()) {
//            FileUtils.deleteDirectory(outDir);
//        }
        boolean status = job.waitForCompletion(true);
        System.exit(status ? 0 : 1);

    }
}
